package com.nkl.page.manager;

import com.nkl.common.dao.BaseDao;
import com.nkl.common.util.DateUtil;
import com.nkl.common.util.Param;
import com.nkl.page.dao.CommentDao;
import com.nkl.page.dao.CommentReplyDao;
import com.nkl.page.dao.InfosDao;
import com.nkl.page.dao.NoteDao;
import com.nkl.page.domain.*;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class IndexManager {

    InfosDao infosDao = new InfosDao();
    NoteDao noteDao = new NoteDao();
    CommentDao commentDao = new CommentDao();
    CommentReplyDao commentReplyDao = new CommentReplyDao();

    /**
     * @Title: listInfoss
     * @Description: 查询最新资讯、学术动态、奖励资助等
     * @param infos
     * @return List<Infos>
     */
    public List<Infos>  listInfoss(Infos infos,int[] sum){
        Connection conn = BaseDao.getConnection();
        if (sum!=null) {
            sum[0] = infosDao.listInfossCount(infos, conn);
        }
        List<Infos> infoss = infosDao.listInfoss(infos,conn);

        BaseDao.closeDB(null, null, conn);
        return infoss;
    }

    /**
     * @Title: getInfos
     * @Description: 查询最新资讯、学术动态、奖励资助详情等
     * @param infos
     * @return Infos
     */
    public Infos  getInfos(Infos infos){
        Connection conn = BaseDao.getConnection();
        Infos _infos = infosDao.getInfos(infos, conn);
        //更新点击率
        _infos.setInfo_click(_infos.getInfo_click()+1);
        infosDao.updateInfos(_infos, conn);
        BaseDao.closeDB(null, null, conn);
        return _infos;
    }

    /**
     * @Title: listNotes
     * @Description: 查询活动公告
     * @param note
     * @return List<Note>
     */
    public List<Note>  listNotes(Note note,int[] sum){
        Connection conn = BaseDao.getConnection();
        if (sum!=null) {
            sum[0] = noteDao.listNotesCount(note, conn);
        }
        List<Note> notes = noteDao.listNotes(note,conn);

        BaseDao.closeDB(null, null, conn);
        return notes;
    }

    /**
     * @Title: getNote
     * @Description: 查询活动公告内容
     * @param note
     * @return Note
     */
    public Note  getNote(Note note){
        Connection conn = BaseDao.getConnection();
        Note _note = noteDao.getNote(note,conn);
        BaseDao.closeDB(null, null, conn);
        return _note;
    }

    /**
     * @Title: listComments
     * @Description: 查询新闻评论信息列表
     * @param comment
     * @return List<Comment>
     */
    public List<Comment>  listComments(Comment comment, int[] sum){
        Connection conn = BaseDao.getConnection();
        if (sum!=null) {
            sum[0] = commentDao.listCommentsCount(comment, conn);
        }
        List<Comment> comments = commentDao.listComments(comment,conn);
        BaseDao.closeDB(null, null, conn);
        return comments;
    }

    /**
     * @Title: getComment
     * @Description: 查询新闻评论
     * @param comment
     * @return Comment
     */
    public Comment getComment(Comment comment){
        Connection conn = BaseDao.getConnection();
        Comment _comment = commentDao.getComment(comment,conn);
        //更新点击率
        _comment.setComment_click(_comment.getComment_click()+1);
        commentDao.updateComment(_comment, conn);
        BaseDao.closeDB(null, null, conn);
        return _comment;
    }

    /**
     * @Title: listCommentReplys
     * @Description: 查询新闻评论回复内容
     * @return List<CommentReply>
     */
    public List<CommentReply>  listCommentReplys(Comment comment){
        Connection conn = BaseDao.getConnection();

        List<CommentReply> list = new ArrayList<CommentReply>();
        CommentReply m1 = new CommentReply();
        m1.setComment_id(comment.getComment_id());
        m1.setComment_reply_pid(-1);
        List<CommentReply> commentReplies = commentReplyDao.listCommentReplys(m1, conn);
        if (commentReplies !=null) {
            for (CommentReply temp : commentReplies) {
                list.add(temp);
                m1.setComment_reply_pid(temp.getComment_reply());
                //listCommentReplysCycle(list, m1, conn);
                List<CommentReply> _commentReplies = commentReplyDao.listCommentReplys(m1, conn);
                if (_commentReplies !=null) {
                    for (CommentReply temp2 : _commentReplies) {
                        list.add(temp2);
                    }
                }

            }
        }


        BaseDao.closeDB(null, null, conn);
        return list;
    }

    /**
     * @Title: addComment
     * @Description: 发布新闻评论标题
     * @param comment
     * @return void
     */
    public void  addComment(Comment comment){
        Connection conn = BaseDao.getConnection();
        //添加信息
        comment.setComment_date(DateUtil.dateToDateString(new Date(), "yyyy-MM-dd HH:mm:ss"));
        //获取ID
        User user = (User)Param.getSession("user");
        comment.setUser_id(user.getUser_id());
        comment.setUpload_pic(comment.getComment_pics());
        commentDao.addComment(comment, conn);
        BaseDao.closeDB(null, null, conn);
    }

    /**
     * @Title: addCommentReply
     * @Description: 添加新闻评论回复
     * @param commentReply
     * @return void
     */
    public void  addCommentReply(CommentReply commentReply){
        Connection conn = BaseDao.getConnection();
        //添加新闻评论回复
        commentReply.setReply_date(DateUtil.dateToDateString(new Date(), "yyyy-MM-dd HH:mm:ss"));
        commentReplyDao.addCommentReply(commentReply, conn);
        BaseDao.closeDB(null, null, conn);
    }

    /**
     * @Title: listCommentReplysCycle
     * @Description: 递归查询新闻评论深层次回复内容
     * @param list
     * @param commentReply
     * @param conn
     * @return void
     */
    private void  listCommentReplysCycle(List<CommentReply> list, CommentReply commentReply, Connection conn){
        List<CommentReply> commentReplies = commentReplyDao.listCommentReplys(commentReply, conn);
        CommentReply m1 = new CommentReply();
        for (CommentReply temp : commentReplies) {
            list.add(temp);
            m1.setComment_reply_pid(temp.getComment_reply());
            listCommentReplysCycle(list, m1, conn);
        }
    }

}
